import { formatTime } from '../../../utils/util.js';
const app = getApp()
Page({

  data: {
    current: 0,
    navList: [
      { id: 0, label: "全部订单", status: "", rederOrderListName: "orderList_all" },
      { id: 1, label: "待付款", status: "已下单，待付款", rederOrderListName: "orderList_waitPay" },
      { id: 2, label: "待审核", status: "已付款，待审核", rederOrderListName: "orderList_waitCheck" },
      { id: 3, label: "待接单", status: "审核通过，待接单", rederOrderListName: "orderList_waitReceive" },
      { id: 4, label: "待发货", status: "已接单，待发货", rederOrderListName: "orderList_waitDelivery" },
    ],
    orderList_all: [],
    orderList_waitPay: [],
    orderList_waitCheck: [],
    orderList_waitReceive: [],
    orderList_waitDelivery: [],
  },

  onLoad: function (options) {
    const current = parseInt(options.current) || 0
    this.handleSwiperChange(current)
    this.setData({current})
  },
  // 获取全部订单列表根据openid
  async getAllOrderListByOpenid() {
    const res = await app.request({
      url: 'getOrder', method: "POST", data: { buyer_openid: app.openid }
    })
    if (res.meta.status >= 300) return wx.showToast({ title: "没有订单呢", icon: 'error' });
    const { order } = res.data
    order.forEach(item => item.create_at = formatTime(item.create_at))
    this.setData({ orderList_all: order })
  },
  // 获取全部订单列表根据 status
  async getAllOrderListByStatus(status, rederOrderListName) {//rederOrderListName字符串，要setData的名字
    const res = await app.request({
      url: 'getOrder', method: "POST", data: { buyer_openid: app.openid, status: status || null }
    })
    if (res.meta.status >= 300) return wx.showToast({ title: "没有订单呢", icon: 'error' });
    const { order } = res.data
    order.forEach(item => item.create_at = formatTime(item.create_at))
    this.setData({ [rederOrderListName]: order })
  },
  // async onDeleteOrder(e) {
  //   const { id } = e.currentTarget.dataset
  //   const modal = await this.showModal({ showCancel: true, confirmText: `仍然删除`, content: `订单支付成功有延迟，请知悉核对如果已经支付，请不要取消订单` })
  //   if (modal.cancel) return
  //   const res = await app.request({
  //     url: 'deleteOrder', method: "POST", data: { id }
  //   })
  //   if (res.meta.status >= 300) return this.showModal({ content: "删除订单失败 " + res.meta.msg })
  //   this.getAllOrderListByOpenid()
  // },
  onNavClick(e) {
    const { current } = e.target.dataset
    this.toggleNav(current)
  },
  // 切换nav
  toggleNav(current) {
    this.setData({ current })
  }
  ,
  handleSwiperChange(e) {
    const { current } = e.detail || this.data
    const currentList = this.data.navList[current]
    if (current == 0) return this.getAllOrderListByOpenid()
    this.getAllOrderListByStatus(currentList.status, currentList.rederOrderListName)
    this.toggleNav(current)
    this.setData({ current })
  },
  showModal(options) {
    return new Promise((resolve, reject) => {
      wx.showModal({
        title: '提示',
        content: '内容',
        showCancel: false,
        confirmText: '知道了',
        confirmColor: '#1B819E',
        ...options,
        success: (res) => {
          resolve(res)
        }
      });
    })
  },
})